package com.informationsystem.model;

import com.informationsystem.model.BaseObject;

import org.apache.commons.lang.StringEscapeUtils;
import org.hibernate.search.annotations.DocumentId;
import org.hibernate.search.annotations.Field;
import org.hibernate.search.annotations.Indexed;
import org.hibernate.search.annotations.IndexedEmbedded;

import java.util.HashSet;
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.MappedSuperclass;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.PrimaryKeyJoinColumn;
import javax.persistence.Table;
import javax.persistence.Transient;

import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;

import javax.xml.bind.annotation.XmlRootElement;

import java.io.Serializable;

@Entity
@Table(name="news")
@Indexed
@XmlRootElement
public class News extends Information implements Serializable {
    private String content;
    

    private PictureNews picturenews;
    @Column(name="content", nullable=false, length=10000)
    @Field
    public String getContent() {
        return this.content;
    }
    
    public void setContent(String content) {
        this.content = content;
    }

    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        News pojo = (News) o;

        if (content != null ? !content.equals(pojo.content) : pojo.content != null) return false;

        return true;
    }

    public int hashCode() {
        int result = 0;
        result = 31 * result + (content != null ? content.hashCode() : 0);

        return result;
    }

    public String toString() {
        StringBuffer sb = new StringBuffer(getClass().getSimpleName());

        sb.append(" [");
        sb.append("id").append("='").append(getId()).append("', ");
        sb.append("content").append("='").append(getContent()).append("', ");
        
        sb.append("]");
      
        return sb.toString();
    }

	@OneToOne(mappedBy = "news",optional = true, cascade = CascadeType.ALL)
	public PictureNews getPicturenews() {
		return picturenews;
	}

	public void setPicturenews(PictureNews picturenews) {
		this.picturenews = picturenews;
	}
	
	@Transient
	public String getShortContent(){
		return stripHtml(getDisplayContent(),200);
	}
	
	@Transient
	public String getPicturenewsContent(){
		return stripHtml(getDisplayContent(),100);
	}
	
	@Transient
	public String stripHtml(String input, int length){
        if (input == null || input.trim().equals("")) {  
            return "";  
        }  
        String str = input.replaceAll("\\&[a-zA-Z]{1,10};", "").replaceAll(  
                "<[^>]*>", "");  
        str = str.replaceAll("[(/>)<]", "");  
        int len = str.length();  
        if (len <= length) {  
            return str;  
        } else {  
            str = str.substring(0, length);  
            str += "......";  
        }  
        return str;  
	}
	@Transient
	public String getDisplayContent(){
		return StringEscapeUtils.unescapeHtml(content);
	}
}
